package cn.tedu.psychology.service.impl;

import cn.tedu.psychology.ex.ServiceException;
import cn.tedu.psychology.mapper.QuestionAnswerMapper;
import cn.tedu.psychology.pojo.dto.QuestionAnswerAddNewDTO;
import cn.tedu.psychology.pojo.entity.QuestionAnswer;
import cn.tedu.psychology.pojo.vo.QuestionAnswerListItemVO;
import cn.tedu.psychology.pojo.vo.QuestionAnswerStandardVO;
import cn.tedu.psychology.service.IQuestionAnswerService;
import cn.tedu.psychology.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionAnswerServiceImpl implements IQuestionAnswerService {

    @Autowired
    private QuestionAnswerMapper questionAnswerMapper;


    @Override
    public void addNew(QuestionAnswerAddNewDTO questionAnswerAddNewDTO) {
        QuestionAnswer questionAnswer = new QuestionAnswer();
        BeanUtils.copyProperties(questionAnswerAddNewDTO, questionAnswer);
        // questionAnswer.setAnswerTime();
        questionAnswerMapper.insert(questionAnswer);
    }

    @Override
    public QuestionAnswerStandardVO getStandardByUserId(Long userId) {
        QuestionAnswerStandardVO questionAnswer = questionAnswerMapper.getStandardByUserId(userId);
        if (questionAnswer == null) {
            String message = "要查看的问题不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return questionAnswer;
    }

    @Override
    public List<QuestionAnswerListItemVO> listByQuestionId(Long questionId) {
        return questionAnswerMapper.listByQuestionId(questionId);
    }
}
